package com.bt7274.service.impl;

import com.bt7274.mapper.ReactionMapper;
import com.bt7274.service.ReactionService;
import com.bt7274.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Data 2025/3/25 02:17
 * @Author hp
 * @Version 1.0
 * @Description UserServiceImpl
 */
@Service
public class ReactionServiceImpl implements ReactionService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ReactionMapper reactionMapper;

    private static final String LIKE_KEY_PREFIX = "post:like:";
    private static final String DISLIKE_KEY_PREFIX = "post:dislike:";

    // 点赞
    public void likePost(String postId) {
        String userId = SessionUtils.getCurrentUserId();
        String likeKey = LIKE_KEY_PREFIX + postId;
        String dislikeKey = DISLIKE_KEY_PREFIX + postId;
        // 点赞
        redisTemplate.opsForSet().add(likeKey, userId);
        redisTemplate.opsForSet().remove(dislikeKey, userId);
        // 有点赞或点踩行为，记录当前 postId 到 dirty Set 中
        redisTemplate.opsForSet().add("reaction:dirty_posts", postId);
        // 同步到数据库
        reactionMapper.insertOrUpdate(postId, userId, 1, LocalDateTime.now());

    }


    // 点踩
    public void dislikePost(String postId) {
        String userId = SessionUtils.getCurrentUserId();
        String likeKey = LIKE_KEY_PREFIX + postId;
        String dislikeKey = DISLIKE_KEY_PREFIX + postId;

        // 添加到点踩 Set，并从点赞 Set 中移除用户
        redisTemplate.opsForSet().add(dislikeKey, userId);
        redisTemplate.opsForSet().remove(likeKey, userId);
        // 有点赞或点踩行为，记录当前 postId 到 dirty Set 中
        redisTemplate.opsForSet().add("reaction:dirty_posts", postId);
        // 同步到数据库
        reactionMapper.insertOrUpdate(postId, userId, -1, LocalDateTime.now());

    }

    // 取消点赞
    public void cancelLike(String postId) {
        String userId = SessionUtils.getCurrentUserId();
        String likeKey = LIKE_KEY_PREFIX + postId;
        redisTemplate.opsForSet().remove(likeKey, userId);
        // 有点赞或点踩行为，记录当前 postId 到 dirty Set 中
        redisTemplate.opsForSet().add("reaction:dirty_posts", postId);
        // 同步到数据库
        reactionMapper.insertOrUpdate(postId, userId, 0, LocalDateTime.now());

    }

    // 取消点踩
    public void cancelDislike(String postId) {
        String userId = SessionUtils.getCurrentUserId();
        String dislikeKey = DISLIKE_KEY_PREFIX + postId;
        redisTemplate.opsForSet().remove(dislikeKey, userId);
        // 有点赞或点踩行为，记录当前 postId 到 dirty Set 中
        redisTemplate.opsForSet().add("reaction:dirty_posts", postId);
        // 同步到数据库
        reactionMapper.insertOrUpdate(postId, userId, 0, LocalDateTime.now());
    }

    // 获取点赞数
    public Integer getLikeCount(String postId) {
        String likeKey = LIKE_KEY_PREFIX + postId;
        Long size = redisTemplate.opsForSet().size(likeKey);
        return size == null ? 0 : Math.toIntExact(size);
    }

    public Integer getDislikeCount(String postId) {
        String dislikeKey = DISLIKE_KEY_PREFIX + postId;
        Long size = redisTemplate.opsForSet().size(dislikeKey);
        return size == null ? 0 : Math.toIntExact(size);
    }

    // 判断用户是否已点赞
    public Boolean checkLikeStatus(String postId) {
        String userId = SessionUtils.getCurrentUserId();
        String likeKey = LIKE_KEY_PREFIX + postId;
        return redisTemplate.opsForSet().isMember(likeKey, userId);
    }

    // 判断用户是否已点踩
    public Boolean checkDislikeStatus(String postId) {
        String userId = SessionUtils.getCurrentUserId();
        String dislikeKey = DISLIKE_KEY_PREFIX + postId;
        return redisTemplate.opsForSet().isMember(dislikeKey, userId);
    }
}




